<?php
class Form_Catalog extends FormProcessor
{
	
	

    
	
	function process($request)
	{
			
		
		global $dbobject;
	
		$this->id= 0;
		
		if (array_key_exists('id', $request)) {
				$this->id= $request['id'];
		}
		
		
		$this->name = trim($request['name']);
	
		if (strlen($this->name) <= 0) 
		{
			$this->addError('name_null','Xin mời nhập name!');
		
		}
		
		
		if ($dbobject->query_exist("select id from catalogs where id = $this->id"))
            {
            	$name_tmp = $dbobject->query_cell("select `name` from `catalogs` where `id` = $this->id",MYSQL_ASSOC,'name');
            	
				if (mb_strtolower($this->name) != mb_strtolower($name_tmp))
				{
			
				if ( $dbobject->code_exist('catalogs','name',$this->name))
				{
				
				$this->addError('name_exist','Catalogs code already exits');
				
				}
				}
			
			}else 
			{
					
			if ( $dbobject->code_exist('catalogs','name',$this->name))
				{
				
				$this->addError('name_exist','Catalogs code already exits');
				
				}
			}
		
	
		
		
		
		
		if (!$this->hasError()) 
		{	
			if (!$dbobject->query_exist("select `id` from `catalogs` where `id` = $this->id"))
            {
			$max = $dbobject->getmaxRanking('catalogs','order');
		
			$sql_add = "
			INSERT INTO `catalogs` ( `name`, `order`) 	VALUES ( '$this->name', $max	)";
			
			
			
			mysql_query($sql_add);
			}
			else 
			{
			
		
			
			$sql_edit ="
			UPDATE `catalogs` SET `name` = '$this->name' where `id` = $this->id Limit 1 ";
			
			
			mysql_query($sql_edit);
		
			
			}
			
		}
		
		
		return !$this->hasError();
		
	
		
		
	
	}
	
	
}
?>